package com.msxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.msxt.annotation.IgnoreAuth;
import com.msxt.dto.NewsEntity;
import com.msxt.result.R;
import com.msxt.service.NewsService;
import com.msxt.utils.MPUtil;
import com.msxt.utils.PageUtils;
import com.msxt.view.NewsView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 公告信息
 * 后端接口
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;


    /**
     * 后端列表
     */
    @GetMapping("/page")
    public R page(@RequestParam Map<String, Object> params,
                  NewsEntity news,
                  HttpServletRequest request) {
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();

        // 添加基础条件
        wrapper.eq("1", "1"); // 确保有基础条件

        // 添加其他查询条件
        MPUtil.likeOrEq(wrapper, news);

        // 处理分页和排序
        PageUtils page = newsService.queryPage(params,
                MPUtil.sort(MPUtil.between(wrapper, params), params));
        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params,
                  NewsEntity news,
                  HttpServletRequest request) {
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();

        // 添加基础条件
        wrapper.eq("1", "1"); // 确保有基础条件

        // 添加其他查询条件
        MPUtil.likeOrEq(wrapper, news);

        // 处理分页和排序
        PageUtils page = newsService.queryPage(params,
                MPUtil.sort(MPUtil.between(wrapper, params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list(NewsEntity news) {
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        wrapper.allEq(MPUtil.allEQMapPre(news, "news"));
        return R.ok().put("data", newsService.list(wrapper));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(NewsEntity news) {
        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        wrapper.allEq(MPUtil.allEQMapPre(news, "news"));
        NewsView newsView = newsService.selectView(wrapper);
        return R.ok("查询公告信息成功").put("data", newsView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        NewsEntity news = newsService.getById(id);
        return R.ok().put("data", news);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {
        NewsEntity news = newsService.getById(id);
        return R.ok().put("data", news);
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody NewsEntity news,
                  HttpServletRequest request) {
        news.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        // ValidatorUtils.validateEntity(news);
        newsService.save(news);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody NewsEntity news,
                 HttpServletRequest request) {
        news.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
        // ValidatorUtils.validateEntity(news);
        newsService.save(news);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody NewsEntity news,
                    HttpServletRequest request) {
        // ValidatorUtils.validateEntity(news);
        newsService.updateById(news);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        newsService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 提醒接口
     */
    @RequestMapping("/remind/{columnName}/{type}")
    public R remindCount(@PathVariable("columnName") String columnName,
                         HttpServletRequest request,
                         @PathVariable("type") String type,
                         @RequestParam Map<String, Object> map) {
        map.put("column", columnName);
        map.put("type", type);

        if("2".equals(type)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date remindStartDate = null;
            Date remindEndDate = null;
            if(map.get("remindstart") != null) {
                Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindStart);
                remindStartDate = c.getTime();
                map.put("remindstart", sdf.format(remindStartDate));
            }
            if(map.get("remindend") != null) {
                Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
                c.setTime(new Date());
                c.add(Calendar.DAY_OF_MONTH, remindEnd);
                remindEndDate = c.getTime();
                map.put("remindend", sdf.format(remindEndDate));
            }
        }

        QueryWrapper<NewsEntity> wrapper = new QueryWrapper<>();
        if(map.get("remindstart") != null) {
            wrapper.ge(columnName, map.get("remindstart"));
        }
        if(map.get("remindend") != null) {
            wrapper.le(columnName, map.get("remindend"));
        }

        long  count= newsService.count(wrapper);
        return R.ok().put("count", count);
    }
}